/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xinxi.dms.view;

import com.xinxi.dms.po.Maintain;
import com.xinxi.dms.po.Room;
import com.xinxi.dms.service.IMaintainService;
import com.xinxi.dms.service.IRoomService;
import com.xinxi.dms.service.MaintainServiceIMPL;
import com.xinxi.dms.service.RoomServiceIMPL;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author zhigu
 */
public class MaintainFrame extends javax.swing.JInternalFrame {
    IRoomService roomService = new RoomServiceIMPL();
    IMaintainService mainService = new MaintainServiceIMPL();
    /**
     * Creates new form NewJInternalFrame
     */
    public MaintainFrame() {
        initComponents();
        List<Maintain> list = mainService.findAll();
        refresh(list);
        this.btnOk.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        boxbuildingno = new javax.swing.JComboBox<>();
        boxunitno = new javax.swing.JComboBox<>();
        boxroomno = new javax.swing.JComboBox<>();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtItem1 = new javax.swing.JTextArea();
        btnsubmit = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane3 = new javax.swing.JScrollPane();
        tabMaintain = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        txtid = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtResult = new javax.swing.JTextField();
        txtPerson = new javax.swing.JTextField();
        jScrollPane5 = new javax.swing.JScrollPane();
        txtItem = new javax.swing.JTextArea();
        btnOk = new javax.swing.JButton();
        btncancel = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jScrollPane4.setViewportView(jTextArea2);

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("维修管理");

        jLabel1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
        jLabel1.setText("保修内容");

        jLabel2.setText("楼号：");

        jLabel3.setText("房间号：");

        jLabel4.setText("单元号：");

        boxbuildingno.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "请选择", "25", "23" }));
        boxbuildingno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxbuildingnoActionPerformed(evt);
            }
        });

        boxunitno.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "请选择" }));
        boxunitno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boxunitnoActionPerformed(evt);
            }
        });

        boxroomno.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "请选择", " " }));

        jLabel5.setText("保修物品：");

        txtItem1.setColumns(20);
        txtItem1.setRows(5);
        jScrollPane1.setViewportView(txtItem1);

        btnsubmit.setText("提交");
        btnsubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsubmitActionPerformed(evt);
            }
        });

        jSeparator1.setForeground(new java.awt.Color(255, 0, 51));

        tabMaintain.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "编号", "楼号", "单元号", "房间号", "保修物品", "保修时间", "维修时间", "维修结果", "维修人"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabMaintain.getTableHeader().setReorderingAllowed(false);
        tabMaintain.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabMaintainMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tabMaintain);
        if (tabMaintain.getColumnModel().getColumnCount() > 0) {
            tabMaintain.getColumnModel().getColumn(0).setResizable(false);
            tabMaintain.getColumnModel().getColumn(1).setResizable(false);
            tabMaintain.getColumnModel().getColumn(2).setResizable(false);
            tabMaintain.getColumnModel().getColumn(3).setResizable(false);
            tabMaintain.getColumnModel().getColumn(4).setResizable(false);
            tabMaintain.getColumnModel().getColumn(5).setResizable(false);
            tabMaintain.getColumnModel().getColumn(6).setResizable(false);
            tabMaintain.getColumnModel().getColumn(7).setResizable(false);
            tabMaintain.getColumnModel().getColumn(8).setResizable(false);
        }

        jLabel6.setText("编号：");

        txtid.setEditable(false);

        jLabel7.setText("维修人：");

        jLabel8.setText("维修结果描述：");

        jLabel9.setText("保修物品：");

        txtItem.setEditable(false);
        txtItem.setColumns(20);
        txtItem.setRows(5);
        jScrollPane5.setViewportView(txtItem);

        btnOk.setText("确定");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });

        btncancel.setText("取消");
        btncancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(107, 107, 107)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(boxbuildingno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(38, 38, 38)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(boxunitno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(boxroomno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(53, 53, 53)
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(btnsubmit)
                                        .addGap(54, 54, 54)))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(38, 38, 38))
            .addComponent(jScrollPane3)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(119, 119, 119)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPerson, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnOk)
                        .addGap(18, 18, 18)
                        .addComponent(btncancel)
                        .addGap(306, 306, 306)))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(boxbuildingno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boxunitno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(boxroomno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnsubmit))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 162, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9))
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtPerson, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel8)
                        .addComponent(jLabel7)
                        .addComponent(txtResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOk)
                    .addComponent(btncancel))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boxunitnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxunitnoActionPerformed
        // TODO add your handling code here:
        this.boxroomno.removeAllItems();
        //向单元号添加一个请选择
        this.boxroomno.addItem("请选择");
        //获取楼号组合框中的内容
        String buildingno = (String)this.boxbuildingno.getSelectedItem();
        String unitno = (String)this.boxunitno.getSelectedItem();
        List<Room> roomlist = roomService.getRoomRoomno(buildingno, unitno);
        for(Room room : roomlist){
            this.boxroomno.addItem(room.getRoomno());
        }
    }//GEN-LAST:event_boxunitnoActionPerformed

    private void boxbuildingnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boxbuildingnoActionPerformed
        // TODO add your handling code here:
        //清空单元号内容
        this.boxunitno.removeAllItems();
        //向单元号添加一个请选择
        this.boxunitno.addItem("请选择");
        //获取楼号组合框中的内容
        String buildingno = (String)this.boxbuildingno.getSelectedItem();
        List<Room> roomlist = roomService.getRoomUnitno(buildingno);
        for(Room room:roomlist){
            //遍历集合
            this.boxunitno.addItem(room.getUnitno());
         //   this.boxroomno.addItem(room.getRoomgno());
        }
    }//GEN-LAST:event_boxbuildingnoActionPerformed

    private void btnsubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsubmitActionPerformed
        // TODO add your handling code here:
        String buildingno = this.boxbuildingno.getSelectedItem().toString();
        String unitno = this.boxunitno.getSelectedItem().toString();
        String roomno = this.boxroomno.getSelectedItem().toString();
        if(buildingno.equals("请选择")||unitno.equals("请选择")||roomno.equals("请选择")){
            JOptionPane.showMessageDialog(this, "请选择楼号单元号房间号，才可提交");
        }else{
        //根据房间号
        Room room = roomService.getRoomid(buildingno, unitno, roomno);
        Integer roomid = room.getRoomid();
        //获取保修物品描述
        String textItem = this.txtItem1.getText();
        
        Maintain main = new Maintain();
        main.setRoomid(roomid);
        main.setReason(textItem);
        boolean result = mainService.add(main);
        if(result)
            JOptionPane.showMessageDialog(this, "保修成功！维修人员将在三天内维修！");
        else
            JOptionPane.showMessageDialog(this, "保修失败！");
        
        List<Maintain> list = mainService.findAll();
        refresh(list);
        cancel();
        }
    }//GEN-LAST:event_btnsubmitActionPerformed

    private void tabMaintainMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabMaintainMouseClicked
        // TODO add your handling code here:
        int row = this.tabMaintain.getSelectedRow();
        this.txtid.setText(this.tabMaintain.getValueAt(row, 0).toString());
        this.txtItem.setText(this.tabMaintain.getValueAt(row, 4).toString());
        
        this.btnOk.setEnabled(true);
        
    }//GEN-LAST:event_tabMaintainMouseClicked

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        // TODO add your handling code here:
        Integer id = Integer.parseInt(this.txtid.getText().trim());
        String maintainreason = this.txtResult.getText().trim();
        String txtItem = this.txtItem.getText().trim();
        String maintainer = this.txtPerson.getText().trim();
        if (maintainreason.equals("")) {
            JOptionPane.showMessageDialog(this, "请输入维修原因");
        } else {
            Maintain main = new Maintain();
            main.setId(id);
            main.setMaintainreason(maintainreason);
            main.setMaintainer(maintainer);
            boolean result = mainService.update(main);
            if (result) {
                JOptionPane.showMessageDialog(this, "维修成功");
            } else {
                JOptionPane.showMessageDialog(this, "维修失败");
            }
            this.btnOk.setEnabled(false);
            List<Maintain> list = mainService.findAll();
            refresh(list);
            cancel();
        }
    }//GEN-LAST:event_btnOkActionPerformed

    private void btncancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncancelActionPerformed
        // TODO add your handling code here:
        cancel();
    }//GEN-LAST:event_btncancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox<String> boxbuildingno;
    private javax.swing.JComboBox<String> boxroomno;
    private javax.swing.JComboBox<String> boxunitno;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton btncancel;
    private javax.swing.JButton btnsubmit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTable tabMaintain;
    private javax.swing.JTextArea txtItem;
    private javax.swing.JTextArea txtItem1;
    private javax.swing.JTextField txtPerson;
    private javax.swing.JTextField txtResult;
    private javax.swing.JTextField txtid;
    // End of variables declaration//GEN-END:variables

    private void refresh(List<Maintain> list) {
        DefaultTableModel dtm = (DefaultTableModel) this.tabMaintain.getModel();
        while(dtm.getRowCount()>0){
            //从第0行开始删除
            dtm.removeRow(0);
        }//遍历集合
        for(Maintain m : list){
            Vector v =new Vector();
            v.add(m.getId());
            v.add(m.getBuildingno());
            v.add(m.getUnitno());
            v.add(m.getRoomno());
            v.add(m.getReason());
            v.add(m.getRepairtime());
            v.add(m.getMaintaintime());
            v.add(m.getMaintainreason());
            v.add(m.getMaintainer());
            dtm.addRow(v);
        }
    }
    private void cancel(){
        this.boxbuildingno.removeAllItems();
        this.boxunitno.removeAllItems();
        this.boxroomno.removeAllItems();
        this.boxbuildingno.addItem("请选择");
        this.boxbuildingno.addItem("23");
        this.boxbuildingno.addItem("25");
        this.boxunitno.addItem("请选择");
        this.boxroomno.addItem("请选择");
        this.txtItem1.setText("");
        this.txtItem.setText("");
        this.txtPerson.setText("");
        this.txtResult.setText("");
        this.txtid.setText("");
        
    }
}
